package com.fc.test.controller.admin;

import com.fc.test.mapper.auto.TAbbCountMapper;
import com.fc.test.model.auto.*;
import com.fc.test.service.TAbbCommissionService;
import com.fc.test.service.TAbbQczifeiService;
import com.fc.test.util.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.fc.test.common.base.BaseController;
import com.fc.test.common.domain.AjaxResult;
import com.fc.test.model.custom.TableSplitResult;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.model.custom.TitleVo;
import com.fc.test.service.TAbbQuanchengService;
import io.swagger.annotations.Api;

import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.util.List;

@Controller
@RequestMapping("TAbbQuanchengController")
@Api(value = "")
public class TAbbQuanchengController extends BaseController{

	private String prefix = "admin/tAbbQuancheng";
	@Autowired
	private TAbbQuanchengService tAbbQuanchengService;
	@Autowired
	private TAbbQczifeiService tAbbQczifeiService;
	@Autowired
	private TAbbCommissionService tAbbCommissionService;
	@Autowired
	private TAbbCountMapper tAbbCountMapper;


	@GetMapping("view")
	@RequiresPermissions("gen:tAbbQuancheng:view")
    public String view(Model model)
    {
		String str="";
		setTitle(model, new TitleVo("列表", str+"管理", true,"欢迎进入"+str+"页面", true, false));
        return prefix + "/list";
    }


	@PostMapping("list")
	@RequiresPermissions("gen:tAbbQuancheng:list")
	@ResponseBody
	public Object list(Tablepar tablepar,String searchTxt){
		PageInfo<TAbbQuancheng> page=tAbbQuanchengService.list(tablepar,searchTxt) ;
		TableSplitResult<TAbbQuancheng> result=new TableSplitResult<TAbbQuancheng>(page.getPageNum(), page.getTotal(), page.getList());
		return  result;
	}

	/**
     * 新增
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        return prefix + "/add";
    }


	@PostMapping("add")
	@RequiresPermissions("gen:tAbbQuancheng:add")
	@ResponseBody
	public AjaxResult add(TAbbQuancheng tAbbQuancheng, Model model, String[] imageUrl, HttpSession session){
    	String images="";
    	for (String image:imageUrl){
            if (images.equals("")){
            	images=images+image;
			}else{
				images=images+","+image;
			}
		}
		tAbbQuancheng.setRealnumber(tAbbQuancheng.getNumber());
		tAbbQuancheng.setImages(images);
		tAbbQuancheng.setPaystatus("2");
		tAbbQuancheng.setCreattime(DateUtil.getCurrentDateTimeStr());

		//查询发布金额
		TAbbQczifeiExample testExample=new TAbbQczifeiExample();
		TAbbQczifei tAbbQczifei=tAbbQczifeiService.SelectLimitone();
		Double balance=Double.parseDouble(tAbbQczifei.getMoney())*tAbbQuancheng.getNumber();

		//算出平台的抽成金额并添加到佣金列表
		TAbbCommission tAbbCommission=new TAbbCommission();
		tAbbCommission.setCreattime(DateUtil.getCurrentDateTimeStr());
		tAbbCommission.setType(3);
		double pingtaiCommission=balance*Integer.parseInt(tAbbQczifei.getPingtaiscore())*0.01;
		DecimalFormat df = new DecimalFormat("#.00");
		String pingtaiCommissionstr = df.format(pingtaiCommission);
		tAbbCommission.setBeiyongone(pingtaiCommissionstr);
		TsysUser user= (TsysUser) SecurityUtils.getSubject().getPrincipal();
		tAbbCommission.setMessage(user.getUsername()+"(系统后台)发布了一个任务:"+tAbbQuancheng.getTitle()+"系统获得了"+pingtaiCommissionstr+"元提成!");
		tAbbCommission.setBusinessid("1");
		tAbbCommissionService.insertSelective(tAbbCommission);

		//减少平台账户的钱
		TAbbCountExample example1=new TAbbCountExample();
		List<TAbbCount> listcount=tAbbCountMapper.selectByExample(example1);
		TAbbCount tAbbCount=listcount.get(0);
		tAbbCount.setCount(tAbbCount.getCount()-balance+pingtaiCommission);
		tAbbCountMapper.updateByPrimaryKey(tAbbCount);

		//添加发布任务的消费记录
		TAbbCommission tAbbCommission1=new TAbbCommission();
		tAbbCommission1.setCreattime(DateUtil.getCurrentDateTimeStr());
		tAbbCommission1.setMessage(user.getUsername()+"(系统后台)发布了一个任务:"+tAbbQuancheng.getTitle()+"系统账户消费了"+balance+"元!");
		tAbbCommission1.setType(3);
		tAbbCommission1.setBusinessid("1");
		tAbbCommission1.setBeiyongone(String.valueOf(-balance));


        //任务总金额等于总钱数减去平台抽成
		tAbbQuancheng.setBalance(balance-pingtaiCommission);
		tAbbQuancheng.setCountblance(balance-pingtaiCommission);
		tAbbQuancheng.setUserid(user.getId());
		tAbbQuancheng.setUsername(user.getUsername());
		int b=tAbbQuanchengService.insertSelective(tAbbQuancheng);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}

	/**
	 * 删除用户
	 * @param ids
	 * @return
	 */
	@PostMapping("remove")
	@RequiresPermissions("gen:tAbbQuancheng:remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b=tAbbQuanchengService.deleteByPrimaryKey(ids);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}


	/**
	 * 修改跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("TAbbQuancheng", tAbbQuanchengService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }

	/**
     * 修改保存
     */
    @RequiresPermissions("gen:tAbbQuancheng:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TAbbQuancheng record)
    {
        return toAjax(tAbbQuanchengService.updateByPrimaryKeySelective(record));
    }



}
